using Avanzis.QueryFramework;
using NUnit.Framework;

namespace Avanzis.QueryBuilder.NUnit
{
	/// <summary>
	/// 
	/// </summary>
	[TestFixture] 
	public class UpdateBuilderTest
	{
        private Dialects previousDialect = Dialects.MSSql2005Dialect;

        [TestFixtureSetUp]
        public void Setup()
        {
            previousDialect = QueryFrameworkConfiguration.Dialect;
            QueryFrameworkConfiguration.Dialect = Dialects.MSSql2000Dialect;
            QueryBuilder.QueryProcessor.ResetProcessor();
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            QueryFrameworkConfiguration.Dialect = previousDialect;
        }

		[Test]
		public void SimpleUpdate()
		{	
			UpdateBuilder sb = new UpdateBuilder();
			sb.TableName = "People";			
			sb.AddFieldValue("FirstName","John");
            sb.AddFieldValue("LastName", "Smith");
			sb.Filters.Add("Active",SqlOperator.Equals,true);		
			
			string result = "UPDATE [People] SET [FirstName] = 'John', [LastName] = 'Smith'\nWHERE (Active = 1)\n";				
			
			Assert.AreEqual(result, sb.ToString());			

			UpdateBuilder cloned = (UpdateBuilder)sb.Clone();
			Assert.AreEqual(cloned.ToString(), sb.ToString());
        }

        [Test]
        public void UpdateWithStrangeCharacters()
        {
            UpdateBuilder sb = new UpdateBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "T.T");
            sb.AddFieldValue("LastName", "John Doe");
            sb.Filters.Add("Active", SqlOperator.Equals, true);

            string result = "UPDATE [People] SET [FirstName] = 'T.T', [LastName] = 'John Doe'\nWHERE (Active = 1)\n";

            Assert.AreEqual(result, sb.ToString());

            UpdateBuilder cloned = (UpdateBuilder)sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }

        [Test]
        public void UpdateWithNullVAlue()
        {
            UpdateBuilder sb = new UpdateBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "T.T");
            sb.AddFieldValue("LastName", null);
            sb.Filters.Add("Active", SqlOperator.Equals, true);

            string result = "UPDATE [People] SET [FirstName] = 'T.T', [LastName] = NULL\nWHERE (Active = 1)\n";

            Assert.AreEqual(result, sb.ToString());

            UpdateBuilder cloned = (UpdateBuilder)sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }

        [Test]
        public void UpdateWithParameters()
        {
            UpdateBuilder sb = new UpdateBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "@FirstName");
            sb.AddFieldValue("LastName", "@LastName");
            sb.Filters.Add("Active", SqlOperator.Equals, true);

            string result = "UPDATE [People] SET [FirstName] = @FirstName, [LastName] = @LastName\nWHERE (Active = 1)\n";

            Assert.AreEqual(result, sb.ToString());
        }


        [Test]
        public void UpdateWithParametersAndValues()
        {
            UpdateBuilder sb = new UpdateBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "@FirstName");
            sb.AddFieldValue("LastName", "John Doe");
            sb.Filters.Add("Active", SqlOperator.Equals, true);

            string result = "UPDATE [People] SET [FirstName] = @FirstName, [LastName] = 'John Doe'\nWHERE (Active = 1)\n";

            Assert.AreEqual(result, sb.ToString());
        }

        [Test]
        public void UpdateWithParametersInWhere()
        {
            UpdateBuilder sb = new UpdateBuilder();
            sb.TableName = "People";
            sb.AddFieldValue("FirstName", "@FirstName");
            sb.AddFieldValue("LastName", "John Doe");
            sb.Filters.Add("Active", SqlOperator.Equals, "@Active");
            sb.Filters.Add("FirstName", SqlOperator.Equals, "T.T");

            string result = "UPDATE [People] SET [FirstName] = @FirstName, [LastName] = 'John Doe'\n" +
                "WHERE (Active = @Active) AND (FirstName = 'T.T')\n";

            Assert.AreEqual(result, sb.ToString());
        }
        
        [Test]
        public void UpdateWithoutFilters()
        {
            UpdateBuilder ub = new UpdateBuilder();
            ub.TableName = "People";
            ub.AddFieldValue("LastName", "John Doe");

            string result = "UPDATE [People] SET [LastName] = 'John Doe'\n";

            Assert.AreEqual(result, ub.ToString());
        }
	}
}
